home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / language / harvest.cpt / Harvest C / tclAE.c < prev    next >
Encoding:
Text File  |  1992-02-11  |  4.1 KB  |  200 lines

  1. // send <'signature'|name> <string> 
  2. tclSend(clientData, interp, argc, argv)
  3. ClientData     clientData;
  4. Tcl_Interp     *interp;
  5. int         argc;
  6. char         **argv;
  7. {
  8.     ProcessSerialNumber        process;
  9.     AppleEvent                theEvent, reply;
  10.     AEAddressDesc            address;
  11.     long                    sig = 0, retType;
  12.     OSErr                    res;
  13.     Size                    sz;
  14.     ProcessInfoRec            infoRec;
  15.  
  16.     if (argc != 3)
  17.     {
  18.         sprintf(interp->result, "Usage: %s <'signature'|name> <string>", argv[0]);
  19.         return TCL_ERROR;
  20.     }
  21.         
  22.     if (argv[1][0] == '\'')
  23.     {
  24.         memmove(&sig, argv[1] + 1, 4);
  25.         if (res = AECreateDesc(typeApplSignature, &sig, sizeof(sig), &address))
  26.         {
  27.             sprintf(interp->result, "Create desc: %d", res);
  28.             return TCL_ERROR;
  29.         }
  30.     }
  31.     else
  32.     {
  33.         char    name[256];
  34.         FSSpec    spec;
  35.         
  36.         process.highLongOfPSN = 0;
  37.         process.lowLongOfPSN = kNoProcess;
  38.         infoRec.processInfoLength = sizeof(ProcessInfoRec);
  39.         infoRec.processName = name;
  40.         infoRec.processAppSpec = &spec;
  41.         CtoPstr(argv[1]);
  42.         while ((res = GetNextProcess(&process)) == noErr)
  43.         {
  44.             if (GetProcessInformation(&process, &infoRec) == noErr)
  45.             {
  46.                 if (!pStrcmp(argv[1], infoRec.processName))
  47.                 {
  48.                     break;
  49.                 }
  50.             }
  51.         }
  52.         PtoCstr(argv[1]);
  53.         if (res) 
  54.         {
  55.             sprintf(interp->result, "No process named '%s'", argv[1]);
  56.             return TCL_ERROR;
  57.         }
  58.         if (res = AECreateDesc(typeProcessSerialNumber, &process, sizeof(process), &address))
  59.         {
  60.             sprintf(interp->result, "Create desc: %d", res);
  61.             return TCL_ERROR;
  62.         }
  63.     }
  64.     
  65.     if (res = AECreateAppleEvent(kTclClass, kAEEval, &address,
  66.         kAutoGenerateReturnID, kAnyTransactionID, &theEvent))
  67.     {
  68.         sprintf(interp->result, "Create apple event err: %d", res);
  69.         return TCL_ERROR;
  70.     }
  71.     
  72.     if (res = AEPutParamPtr(&theEvent, keyDirectObject, typeCString, argv[2], 
  73.         strlen(argv[2]) + 1))
  74.     {
  75.         sprintf(interp->result, "AEPut parameter error: %d", res);
  76.         return TCL_ERROR;
  77.     }
  78.     
  79.     if (res = AESend(&theEvent, &reply, kAEWaitReply + kAECanInteract, 
  80.         kAENormalPriority, 1200, NULL, NULL))
  81.     {
  82.         sprintf(interp->result, "AESend error: %d", res);
  83.         return TCL_ERROR;
  84.     }
  85.     
  86.     if (res = AEGetParamPtr(&reply, keyDirectObject, typeCString, &retType,
  87.         interp->result, TCL_RESULT_SIZE, &sz))
  88.     {
  89.         sprintf(interp->result,"AEGetParam error: %d", res);
  90.         return TCL_ERROR;
  91.     }
  92.     
  93.     return TCL_OK;
  94. }
  95.  
  96.  
  97. pascal OSErr
  98. aeTclEval(event, reply, ref)
  99. AppleEvent    *event, *reply;
  100. long        ref;
  101. {
  102.     AEDescList    docList;
  103.     OSErr        res;
  104.     OSErr        retVal;
  105.     long        index, itemsInList;
  106.     long        sz;
  107.     AEKeyword    keyWord;
  108.     DescType    returnedType;
  109.     char        buffer[256];
  110.     
  111.     if (res = AEGetParamDesc(event, keyDirectObject, typeAEList, &docList))
  112.     {
  113.         SysBeep(1);
  114.         return res;
  115.     }
  116.  
  117.     /* check for missing params */
  118.     if (res = MyGotRequiredParams(event))
  119.     {
  120.         SysBeep(1);
  121.         return res;
  122.     }
  123.  
  124.     if (res = AEGetNthPtr(&docList, 1, typeCString, &keyWord,
  125.                     &returnedType, buffer, sizeof(buffer),&sz))
  126.     {
  127.         SysBeep(1);
  128.         return res;
  129.     }
  130.     AEDisposeDesc(&docList);
  131.     
  132.     // Evaluate the string and return the result.
  133.     retVal = Tcl_Eval(interp, buffer, 0, NULL);
  134.     res = AEPutParamPtr(reply, keyDirectObject, typeCString, interp->result,
  135.                         strlen(interp->result) + 1);
  136.     return retVal;
  137. }
  138.  
  139.  
  140.  
  141. tclSwitch(clientData, interp, argc, argv)
  142. ClientData     clientData;
  143. Tcl_Interp     *interp;
  144. int         argc;
  145. char         **argv;
  146. {
  147.     ProcessSerialNumber        process;
  148.     ProcessInfoRec            infoRec;
  149.     FSSpec                    spec;
  150.     char                    name[256];
  151.     long                    sig;
  152.     
  153.     if (argc != 2)
  154.     {
  155.         sprintf(interp->result, "Usage: %s <name-of-app|'signature'>", argv[0]);
  156.         return TCL_ERROR;
  157.     }
  158.     if (argv[1][0] == '\'')
  159.     {
  160.         strncpy(&sig, argv[1] + 1, 4L);
  161.     }
  162.     else
  163.     {
  164.         sig = 0L;
  165.         CtoPstr(argv[1]);
  166.     }
  167.     
  168.     process.highLongOfPSN = 0;
  169.     process.lowLongOfPSN = kNoProcess;
  170.     infoRec.processInfoLength = sizeof(ProcessInfoRec);
  171.     infoRec.processName = name;
  172.     infoRec.processAppSpec = &spec;
  173.     while (GetNextProcess(&process) == noErr)
  174.     {
  175.         if (GetProcessInformation(&process, &infoRec) == noErr)
  176.         {
  177.             if (sig)
  178.             {
  179.                 if ((infoRec.processType == 'APPL') && 
  180.                     (infoRec.processSignature == sig))
  181.                 {
  182.                     SetFrontProcess(&process);
  183.                     return TCL_OK;
  184.                 }
  185.             }
  186.             else
  187.             {
  188.                 if (!pStrcmp(argv[1], infoRec.processName))
  189.                 {
  190.                     SetFrontProcess(&process);
  191.                     return TCL_OK;
  192.                 }
  193.             }
  194.         }
  195.     }
  196.     sprintf(interp->result, "Unable to find your process");
  197.     return TCL_ERROR;
  198. }
  199.  
  200.